<%@ page import="java.sql.*, javax.sql.*, javax.naming.*, java.text.*" %>
<%@page import="support.*, java.util.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Applications</title>

<script type="text/javascript">
function showApp(studentid){
    var xmlHttp;
      xmlHttp=new XMLHttpRequest();

      var responseHandler = function()
       {
        if(xmlHttp.readyState==4)
         { document.getElementById(("application" + studentid)).innerHTML = xmlHttp.responseText; }
       }

      xmlHttp.onreadystatechange = responseHandler ;

      xmlHttp.open("GET","getReviewInfo.jsp?studentid=" + studentid,true);
      xmlHttp.send(null);
  
}

function hideApp(studentid){
    document.getElementById(("application" + studentid)).innerHTML = "<input type=\"button\" name=\"showapp\" value=\"Show Application\" onclick=\"showApp('" + studentid +  "')\" >";
}
</script>

</head>
<body>
    <% // Constants for SQL type checking, found SQL type codes online at http://download.oracle.com/javase/1.4.2/docs/api/constant-values.html#java.sql.Types.DOUBLE
      final int NULL = 0;
      final int INTEGER = 4;
      final int DOUBLE = 8;
      final int STRING = 12;
      ArrayList<String> decisions = new ArrayList<String>();
      decisions.add("Accept");
      decisions.add("Reject");
      decisions.add("Cancel");
      // get grades
      ArrayList<Double> grades = (ArrayList<Double>)request.getSession(false).getAttribute("grades");
      // get format for grades
      DecimalFormat df = new DecimalFormat("#.##");
    %>
    <!-- Book keeping to see who called this page -->
    <%
    String analytic = (String)request.getSession(false).getAttribute("choice");
    String category = request.getParameter("category");
    %>
    
    <%
    
    ResultSet rset = null;
    // keeps track of columns in the table
    ResultSetMetaData rsetMeta;
    int numCol = 0;
        
        // analytic if path is discipline
        if(analytic.equals("discipline")){
          out.print("You came from Disciplines");
        } 
        
        // analytic if path is specialization
        if(analytic.equals("specialization")){
          out.print("You came from Specializations <br> <br>");
        }
        
        if(analytic.equals("reviewer")){
            out.print("You came from Reviewer <br> <br>");
          }
        
        rset = (ResultSet)request.getSession(false).getAttribute("application");
        // checks if rset was properly set
        if(rset != null){

            // get number of columns
            rsetMeta = rset.getMetaData();
            numCol = rsetMeta.getColumnCount();

            // make table/columns
            out.print("<table border=\"1\">");
            out.print("<tr><td></td><td></td>");
            // print column names/labels
            for (int i =0; i<numCol; i++){
                out.print("<td>" + rsetMeta.getColumnLabel(i+1) + "</td>");
            }
            out.print("<td>Average</td>");
            out.print("<td>Application</td></tr>");
 
            int gradeCounter = 0;
            // print applications
            while(rset.next()){
                int StudentId = 0;
                out.print("<tr>");
                // form for the submit buttons
                for(int i = 0; i < decisions.size(); i++){
                    if(!decisions.get(i).equals(rset.getString("decision")) && !(decisions.get(i).equals("Cancel") && rset.getString("decision").equals("Pending"))){
                        out.print("<td><form name=\"decision\" action=\"/Application_Submission/admin/chairDecision.do\" method=\"get\">");
                        out.print("<input type='hidden' name='decision' value=\"" + decisions.get(i) + "\"/>");
                        out.print("<input type='hidden' name='studentid' value=\"" + rset.getInt("pid") + "\"/>");
                        out.print("<input type='submit' value=\"" + decisions.get(i)  + "\"/>");
                        out.print("</form></td>");
                   }
                }
                
                // loop through columns
                for(int i = 0; i < numCol; i++){
                    out.print("<td>");
                    // type checks how to handle tuple information
                    switch(rsetMeta.getColumnType(i+1)){
                      case NULL:
                          out.print("None");
                          break;
                      case INTEGER:
                          if(i==0){
                              StudentId = rset.getInt(i+1);
                              out.print(StudentId);
                          }else{
                              out.print(rset.getInt(i+1));
                          }
                          break;
                      case DOUBLE:
                          out.print(rset.getDouble(i+1));
                          break;
                      case STRING:
                          // checks if string is null (Strings have optional fields)
                          if(rset.getString(i+1) == null)
                              out.print("None");
                          else
                              out.print(rset.getString(i+1));
                          break;
                      default:
                          if(rset.getString(i+1) == null)
                              out.print("None");
                          else
                              out.print(rset.getString(i+1));
                    }
                    out.print("</td>");
                }
                // print out average grade
                out.print("<td>" + df.format(grades.get(gradeCounter)) + "</td>");
                gradeCounter++;
                // form for the show buttons
                out.print("<td><form name=\"show\" method=\"get\">");
                out.print("<span id=\"application" + StudentId + "\"><input type='button' name='showapp' value='Show Application' onclick=\"showApp('" + StudentId +  "');\"");
                out.print("</span></form></td>");
                out.print("</tr>");
            }
            out.print("</table");
        }
    %>
    
</body>
</html>